$(function () {
    let schoolId = getQueryString("schoolId");
    let url = 'lanling/schoolevaluate/list';
    if (schoolId) {
        url += '?schoolId=' + schoolId;
    }
    $("#jqGrid").jqGrid({
        url: baseURL + url,
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '学校', name: 'schoolName', index: 'school_name', width: 80, align: "center"},
            {label: '教学星', name: 'teachStar', index: 'teach_star', width: 80, align: "center"},
            {label: '环境星', name: 'envStar', index: 'env_star', width: 80, align: "center"},
            {label: '收费星', name: 'payStar', index: 'pay_star', width: 80, align: "center"},
            {label: '服务评价星数量', name: 'serverStar', index: 'server_star', width: 80, align: "center"},
            {label: '评价内容', name: 'contentDesc', index: 'content_desc', width: 80, align: "center"},
            {label: '用户', name: 'studentName', index: 'student_name', width: 80, align: "center"},
            {label: '评论时间', name: 'createDate', index: 'create_date', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        schoolEvaluate: {
            teachStar: 5,
            envStar: 5,
            payStar: 5,
            serverStar: 5,
        },
        ruleValidate: {
            name: [
                {required: true, message: 'name不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ]
        },
        q: {
            name: ''
        },
        schoolList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.schoolEvaluate = {
                teachStar: 5,
                envStar: 5,
                payStar: 5,
                serverStar: 5,
            };
            vm.getSchoolList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSchoolList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.schoolEvaluate.id == null ? "lanling/schoolevaluate/save" : "lanling/schoolevaluate/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.schoolEvaluate),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/schoolevaluate/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/schoolevaluate/info/" + id, function (r) {
                vm.schoolEvaluate = r.schoolEvaluate;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSchoolList: function (id) {
            $.get(baseURL + "lanling/school/queryAll", function (r) {
                vm.schoolList = r.list;
            });
        },
        schoolChange: function (val) {
            //vm.schoolEvaluate = val.label.trim();
        }
    }
});